﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Job.Web.Framework;
using Job.Logic.Personal;
using Job.Entity;
using Job.Logic.Common;
using Job.Web.RelatedDrowDownList;
using System.Collections;
using System.Data;

namespace Job.Web.userControls.Personal
{
    public partial class PersonalBaseInfo : BaseCtrl
    {
        int _personInfoId = myR["resumeid"].IntValue;
        JobPersonInfo info = new JobPersonInfo();
        PersonalLogic pl = new PersonalLogic();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                InitData();
                LoadData();
            }
        }

        private void InitData()
        {
            //初始化月份
            for (int i = 1; i <= 12; i++)
            {
                ddlBornMonth.Items.Add(new ListItem(i.ToString(), i.ToString()));
                ddlGradMonth.Items.Add(new ListItem(i.ToString(), i.ToString()));
            }

            //初始化年份
            for (int i = 1911; i <= 2099; i++)
            {
                ddlBornYear.Items.Add(new ListItem(i.ToString(), i.ToString()));
                ddlGradYear.Items.Add(new ListItem(i.ToString(), i.ToString()));
            }

            //初始化天
            for (int i = 1; i <= 31; i++)
            {
                ListItem item = new ListItem(i.ToString(), i.ToString());
                ddlBornDay.Items.Add(item); 
            }
        }
        private void LoadData()
        {
            try
            {
                info = pl.GetSinglePersonBaseInfo(_personInfoId);
                if (info != null)
                {
                    if (info.UserSex.HasValue)
                    {
                        rblSex.SelectedValue = info.UserSex.Value.ToString();
                    }

                    if (info.SecrecySet.HasValue)
                    {
                        rblSecurity.SelectedValue = info.SecrecySet.Value.ToString();
                    }

                    txtSkipCompany.Text = info.SkipCompany;
                    txtworkq.Text = info.WorkQualify;
                    txtName.Text = info.UserName;
                    txtPhone.Text = info.LinkPhone;
                    txtEmail.Text = info.LinkEmail;
                    txtCardId.Text = info.UserCard;
                    txtAddress.Text = info.ContactAddr;
                    txtIndex.Text = info.HomePage;
                    txtMobile.Text = info.LinkMobile;
                    txtPostCode.Text = info.ContactPostcode;
                    txtqq.Text = info.QqMsn;
                    txtScool.Text = info.GradSchoole;
                    int _bornYear = info.BirthDate.HasValue ? info.BirthDate.Value.Year : 1980;
                    int _bornMonth = info.BirthDate.HasValue ? info.BirthDate.Value.Month : 1;
                    int _bornDay = info.BirthDate.HasValue ? info.BirthDate.Value.Day : 1; //info.BirthDate.Value.Day;
                    if (ddlBornDay.Items.FindByValue(_bornDay.ToString()) != null)
                    {
                        ddlBornDay.SelectedValue = _bornDay.ToString();
                    }

                    if (ddlBornMonth.Items.FindByValue(_bornMonth.ToString()) != null)
                    {
                        ddlBornMonth.SelectedValue = _bornMonth.ToString();
                    }

                    if (ddlBornYear.Items.FindByValue(_bornYear.ToString()) != null)
                    {
                        ddlBornYear.SelectedValue = _bornYear.ToString();
                    }

                    string _gMonth = info.GradMonth.HasValue ? info.GradMonth.Value.ToString() : "";
                    if (ddlGradMonth.Items.FindByValue(_gMonth) != null)
                    {
                        ddlGradMonth.SelectedValue = _gMonth;
                    }

                    string _gYear = info.GradYear.HasValue ? info.GradYear.Value.ToString() : "";

                    if (ddlGradYear.Items.FindByValue(_gYear) != null)
                    {
                        ddlGradYear.SelectedValue = _gYear;
                    }

                    if (info.UserHeight.HasValue && info.UserHeight.Value > 0)
                    {
                        txtTall.Text = info.UserHeight.Value.ToString();// GetValueOrDefault().ToString();
                    }

                    if (info.WorkLen.HasValue && info.WorkLen.Value >0)
                    {
                        txtWorkLen.Text = info.WorkLen.Value.ToString();
                    }
                    //txtother.Text = info.OtherCertify;
                    //计算机水平
                    string comLevel = string.Empty;
                    if (info.ComputerSkill.HasValue)
                        comLevel = info.ComputerSkill.Value.ToString();
                    WebCommon.SetDropDownFromDict(ddlComputerLevel, comLevel, Constants.GetDictionaryCode(Constants.DictionaryType.LANG_SKILL), true);
                    //学历
                    string highEdu = string.Empty;
                    if (info.HighEdu.HasValue)
                        highEdu = info.HighEdu.Value.ToString();
                    WebCommon.SetDropDownFromDict(ddlHighEdu, highEdu, Constants.GetDictionaryCode(Constants.DictionaryType.EDUCATION_LEVEL), true);

                    //民族
                    string nation = string.Empty;
                    if (info.UserNationality.HasValue)
                        nation = info.UserNationality.Value.ToString();
                    WebCommon.SetDropDownFromDict(ddlNation, nation, Constants.GetDictionaryCode(Constants.DictionaryType.PERSON_NATIONALITY), true);



                    //婚姻状态
                    string marryState = string.Empty;
                    if (info.MarryState.HasValue)
                        marryState = info.MarryState.Value.ToString();
                    WebCommon.SetDropDownFromDict(ddlMarried, marryState, Constants.GetDictionaryCode(Constants.DictionaryType.MARRY_STATE), true);

                    //专业一
                    WebCommon.LoadLevelDropDownList(ddlSpec1, ddlSpec11, RelatedListSpec1, Constants.GetParentCodeBySubCode(info.SpecialtyClass1), info.SpecialtyClass1, Constants.DictionaryType.SPECIALTY_CLASS);
                    //专业二
                    WebCommon.LoadLevelDropDownList(ddlSpec2, ddlSpec22, RelatedListSpec2, Constants.GetParentCodeBySubCode(info.SpecialtyClass2), info.SpecialtyClass2, Constants.DictionaryType.SPECIALTY_CLASS);
                    //户口所在地
                    WebCommon.LoadLevelDropDownList(ddlHomeAddress1, ddlHomeAddress2, HomeAddressRlist, Constants.GetParentCodeBySubCode(info.HomeAddr), info.HomeAddr, Constants.DictionaryType.ADDR_CITY);

                    //居住地
                    WebCommon.LoadLevelDropDownList(ddlResideAddress1, ddlResideAddress2, ResideAddressRList, Constants.GetParentCodeBySubCode(info.ResideAddr), info.ResideAddr, Constants.DictionaryType.ADDR_CITY);

                    //语言种类
                    string langType = string.Empty;
                    if (info.ForeignLang.HasValue)
                        langType = info.ForeignLang.Value.ToString();
                    WebCommon.SetDropDownFromDict(ddlLanguagetype, langType, Constants.GetDictionaryCode(Constants.DictionaryType.FOREIGN_LANG), true);

                    //外语熟练程度1
                    string langSkill = string.Empty;
                    if (info.ForeignSkill.HasValue)
                        langSkill = info.ForeignSkill.Value.ToString();
                    WebCommon.SetDropDownFromDict(dlllanguageSkill, langSkill, Constants.GetDictionaryCode(Constants.DictionaryType.LANG_SKILL), true);

                    //外语证书
                    string langbook = string.Empty;
                    if (info.ForeignCertify.HasValue)
                        langbook = info.ForeignCertify.Value.ToString();
                    WebCommon.SetDropDownFromDict(ddllanBook, langbook, Constants.GetDictionaryCode(Constants.DictionaryType.LANG_CERTIFY), true);

                    //外语熟练程度1
                    string yueSkill = string.Empty;
                    if (info.DialectLang.HasValue)
                        yueSkill = info.DialectLang.Value.ToString();
                    WebCommon.SetDropDownFromDict(ddlLanYueSKill, yueSkill, Constants.GetDictionaryCode(Constants.DictionaryType.LANG_SKILL), true);

                    //工作状态
                    string status = string.Empty;
                    if (info.JobStatus.HasValue)
                        status = info.JobStatus.Value.ToString();
                    WebCommon.SetDropDownFromDict(ddlWrkStatus, status, Constants.GetDictionaryCode(Constants.DictionaryType.CURRENT_WORKSTATUS), false);


                    //技术职称
                    string techTitle = string.Empty;
                    if (info.TechTitle.HasValue)
                        techTitle = info.TechTitle.Value.ToString();
                    WebCommon.SetDropDownFromDict(ddlTechTitle, techTitle, Constants.GetDictionaryCode(Constants.DictionaryType.TECH_TITLE), true);
                }
            }
            catch (Exception ex)
            {
                ShowMessage("查询失败");
                Logger.WriteLog("PersonalBaseInfo--LoadData\r\n"+ex.Message);
            }
        }


        protected void btnSave_Click(object sender, EventArgs e)
        {
            string ctrlName = "";
            //新建数据
            try
            {
                //bool isNew = false;
                //if (info == null)
                //{
                //    info = new JobPersonInfo();
                //    isNew = true;
                //}
                info = pl.GetSinglePersonBaseInfo(_personInfoId);
                info.SecrecySet = Convert.ToInt32(rblSecurity.SelectedValue);
                info.SkipCompany = txtSkipCompany.Text.Trim();
                info.ResideAddr = ddlResideAddress2.SelectedValue.Trim();
                info.HomeAddr = ddlHomeAddress2.SelectedValue.Trim();
                DateTime birthdate = new DateTime(Convert.ToInt32(ddlBornYear.SelectedValue), Convert.ToInt32(ddlBornMonth.SelectedValue), Convert.ToInt32(ddlBornDay.SelectedValue));

                //Convert.ToDateTime(ddlBornYear.SelectedValue.Trim() + "-" + ddlBornMonth.SelectedValue.Trim() + "-" + ddlBornDay.SelectedValue.Trim());
                info.BirthDate = birthdate;
                int cskill = -1;
                cskill = GetDictIdFromDropDown(ddlComputerLevel);
                if (cskill < 0)
                    ctrlName += "电脑水平,";
                info.ComputerSkill = cskill;
                info.ContactAddr = txtAddress.Text.Trim();
                info.ContactPostcode = txtPostCode.Text.Trim();
                //粤语水平
                int dlang = -1;
                dlang = GetDictIdFromDropDown(ddlLanYueSKill);
                if (dlang < 0)
                    ctrlName += "粤语水平,";
                info.DialectLang = dlang;

                //外语证书
                int fcer = -1;
                fcer = GetDictIdFromDropDown(ddllanBook);
                if (fcer < 0)
                    ctrlName += "外语证书,";
                info.ForeignCertify = fcer;
                //外语种类
                int flang = -1;
                flang = GetDictIdFromDropDown(ddlLanguagetype);
                if (flang < 0)
                    ctrlName += "外语种类,";
                info.ForeignLang = flang;// Convert.ToInt32(ddlLanguagetype.SelectedValue);
                //外语水平
                int fskill = -1;
                fskill = GetDictIdFromDropDown(dlllanguageSkill);
                if (fskill < 0)
                    ctrlName += "外语水平,";
                info.ForeignSkill = fskill;// Convert.ToInt32(dlllanguageSkill.SelectedValue);

                info.GradMonth = Convert.ToInt32(ddlGradMonth.SelectedValue.Trim());
                info.GradSchoole = txtScool.Text.Trim();
                info.GradYear = Convert.ToInt32(ddlGradYear.SelectedValue.Trim());
                info.HideCard = chkHideId.Checked;
                int Hedu = -1;
                Hedu = GetDictIdFromDropDown(ddlHighEdu);
                if (Hedu < 0)
                    ctrlName += "最高学历,";

                info.HighEdu = Hedu;// Convert.ToInt32(ddlHighEdu.SelectedValue.Trim());
                info.HomePage = txtIndex.Text.Trim();
                info.LastRefresh = DateTime.Now;
                info.LinkEmail = txtEmail.Text.Trim();
                info.LinkMobile = txtMobile.Text.Trim();
                info.LinkPhone = txtPhone.Text.Trim();

                int mState = -1;
                mState = GetDictIdFromDropDown(ddlMarried);
                if (mState < 0)
                    ctrlName += "婚姻状况,";
                info.MarryState = mState;// Convert.ToInt32(ddlMarried.SelectedValue);
                //其它证书
                //info.OtherCertify = txtother.Text.Trim();
                info.QqMsn = txtqq.Text.Trim();
                info.SpecialtyClass1 = ddlSpec11.SelectedValue.Trim();
                info.SpecialtyClass2 = ddlSpec22.SelectedValue.Trim();
                //专业名称
                //info.SpecialtyManual
                int tTitle = -1;
                tTitle = GetDictIdFromDropDown(ddlTechTitle);
                if (tTitle < 0)
                    ctrlName += "职称,";

                info.TechTitle = tTitle;// Convert.ToInt32(ddlTechTitle.SelectedValue.Trim());
                info.UserCard = txtCardId.Text.Trim();
                if (txtTall.Text.Trim().Length >0)
                {
                    info.UserHeight = Convert.ToInt32(txtTall.Text.Trim());
                }
                else
                {
                    info.UserHeight = 0;
                }
                info.UserName = txtName.Text.Trim();
                int unation = -1;
                unation = GetDictIdFromDropDown(ddlNation);
                if (unation < 0)
                    ctrlName += "民族,";

                info.UserNationality = unation;// Convert.ToInt32(ddlNation.SelectedValue.Trim());

                info.UserSex = Convert.ToInt32(rblSex.SelectedValue.Trim());

                if (txtWorkLen.Text.Trim().Length > 0)
                {
                    info.WorkLen = Convert.ToInt32(txtWorkLen.Text.Trim());
                }
                else
                {
                    info.WorkLen = 0;
                }
                int jstatus = -1;
                jstatus = GetDictIdFromDropDown(ddlWrkStatus);
                if (jstatus < 0)
                    ctrlName += "工作状态,";
                info.JobStatus = jstatus;// Convert.ToInt32(ddlWrkStatus.SelectedValue);
                //执业资格
                info.WorkQualify = txtworkq.Text.Trim();
                if (ctrlName != "")
                {
                    ShowMessage("保存失败：<br> '" + ctrlName + "' 不能为空");
                    return;
                }
                //if (isNew)
                //{
                //    pl.Insert(info);
                //}
                //else
                //{
                info.PersonInfoId = _personInfoId;
                pl.Update(info);
                //}
                ShowMessage("保存成功!");
            }
            catch (Exception ex)
            {
                ShowMessage("保存失败：<br>" + ex.Message);
                Logger.WriteLog("个人简历保存失败：" + ex.Message);
            }
        }

        private int GetDictIdFromDropDown(DropDownList ddlcontrol)
        {
            int _result = -1;
            if (Int32.TryParse(ddlcontrol.SelectedValue.Trim(), out _result))
            {
                return _result;
            }
            else
            {
                return -1;
            }
        }
        private void ShowMessage(string message)
        {
            this.message.InnerHtml = "<font color=red>" + message + "</font>";
            this.message.Visible = true;
            this.message.Height = "30px";
            this.message.VAlign = "middle";
        }
    }
}